//
//  ZYCTeaAttendanceRecordViewController.m
//  ZDSC
//
//  Created by 张威 on 2021/10/19.
//

#import "ZYCTeaAttendanceRecordViewController.h"
#import "ZYCTeaAttendanceRecordBottomView.h"
#import "ZYCTeaAttendanceClockInView.h"
#import "ZYCTeaAttendanceStatisticsView.h"
#import "ZYCUserLoginModel.h"
#import "ZYCTeaLeaveRecordModel.h"
#import "ZYCTeaLeaveStatisticsModel.h"
#import "NSDateExtra.h"
#import "ZYCTeaAttendanceClockInStatisticsView.h"

@interface ZYCTeaAttendanceRecordViewController ()<ZYCTeaAttendanceRecordBottomViewDelegate,ZYCTeaAttendanceClockInViewDelegate,ZYCTeaAttendanceStatisticsViewDelegate>


@property (nonatomic, strong) ZYCTeaAttendanceRecordBottomView *bottomView;
// 打卡
@property (nonatomic, strong) ZYCTeaAttendanceClockInView *clockInView;
// 统计
@property (nonatomic, strong) ZYCTeaAttendanceStatisticsView *statisticsView;

@property (nonatomic, strong) NSDateFormatter *dateFormatter;
@property (nonatomic, copy) NSString *attendanceDate;
@property (nonatomic, copy) NSString *teacherId;

@property (nonatomic, strong) NSArray *normalArr;
@property (nonatomic, strong) NSArray *exceptArr;
@property (nonatomic, strong) NSArray *workOvertimeArr;


@property (nonatomic, copy) NSString *headerString;

@end

@implementation ZYCTeaAttendanceRecordViewController
#pragma mark - 生命周期函数
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"考勤记录";
    [self.view addSubview:self.clockInView];
    [self.view addSubview:self.statisticsView];
    [self.view addSubview:self.bottomView];
    
    self.attendanceDate = [self.dateFormatter stringFromDate:[NSDate date]];
    self.headerString = [NSString stringWithFormat:@"%@", [NSDateExtra getDateStringWithDateString:self.attendanceDate currenDateFormat:@"yyyy-MM-dd" dateFormat:@"yyyy年MM月"]];
    self.statisticsView.headerString = self.headerString;
    ZYCUserLoginModel *userLogin = [ZYCUserLoginModel userLogin];
    self.teacherId = userLogin.userId;
    
    [self loadCalendarList];
    [self getTeacherAttendanceByDay:self.attendanceDate];
    [self loadTeaMonthAttendanceStatisc];
    
}

#pragma mark - 网络请求
// 获取日历数据
- (void)loadCalendarList{
    NSDictionary *params = @{
        @"attendanceDate":self.attendanceDate,
        @"teacherId":self.teacherId
    };
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_TeacherMonthListIOS requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            if (SUCCESS(responseObject[@"code"])) {
                self.clockInView.normalArr = responseObject[@"body"][@"attendanceIosListZC"];
                self.clockInView.workOvertimeArr = responseObject[@"body"][@"attendanceIosListJB"];
                self.clockInView.exceptArr = responseObject[@"body"][@"attendanceIosListYC"];
                
                [self.clockInView reloadCalendarData];
            }else{
                [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
            }
        } failure:^(NSError * _Nonnull error) {
            
        }];
}

// 获取考勤情况
- (void)getTeacherAttendanceByDay:(NSString *)day{
    NSDictionary *params = @{
        @"attendanceDate":day,
        @"teacherId":self.teacherId
    };
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_TeacherDayRecord requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [MrjSVProgressHUD dismiss];
            if (SUCCESS(responseObject[@"code"])) {
                if ([responseObject[@"body"] isKindOfClass:[NSDictionary class]]) {
                    // 1: 需要考勤 2: 不需要考勤(今日无考勤)
                    NSString *isNeedCard = responseObject[@"body"][@"isNeedCard"];
                    if ([isNeedCard integerValue] == 2) {
                        self.clockInView.staticsBottomView.hidden = YES;
                        self.clockInView.tipLabel.hidden = NO;
                    }else{
                        ZYCTeaLeaveRecordModel *model = [ZYCTeaLeaveRecordModel yy_modelWithDictionary:responseObject[@"body"]];
                        self.clockInView.staticsBottomView.hidden = NO;
                        self.clockInView.tipLabel.hidden = YES;
                        self.clockInView.model = model;
                    }
                }else{
                    self.clockInView.staticsBottomView.hidden = YES;
                    self.clockInView.tipLabel.hidden = NO;
                }
      
            }else{
                [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
            }
        } failure:^(NSError * _Nonnull error) {
            
        }];
}


// 考勤统计
- (void)loadTeaMonthAttendanceStatisc{
    NSDictionary *params = @{
        @"attendanceDate":self.attendanceDate,
        @"teacherId":self.teacherId
    };

    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_TeacherMonthAttendance requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [MrjSVProgressHUD dismiss];
            if (SUCCESS(responseObject[@"code"])) {
                ZYCTeaLeaveStatisticsModel *model = [ZYCTeaLeaveStatisticsModel yy_modelWithDictionary:responseObject[@"body"]];
                self.statisticsView.model = model;
            }else{
                [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
            }
        } failure:^(NSError * _Nonnull error) {
            
        }];
}


#pragma mark - ZYCTeaAttendanceStatisticsViewDelegate
- (void)teaAttendanceStatisticsViewDidExchangeMonth:(NSString *)timeStr{
    self.attendanceDate = timeStr;
    [self loadTeaMonthAttendanceStatisc];
}


#pragma mark - ZYCTeaAttendanceClockInViewDelegate
- (void)teaAttendanceClockInViewDidSelectDate:(NSString *)dateStr{
    [self getTeacherAttendanceByDay:dateStr];
    
}
#pragma mark - ZYCTeaAttendanceRecordBottomViewDelegate
- (void)teaAttendanceRecordBottomViewDidClick:(NSInteger)index{
    if (index == 0) {
        self.clockInView.hidden = NO;
        self.statisticsView.hidden = YES;
    }else{
        self.clockInView.hidden = YES;
        self.statisticsView.hidden = NO;
    }
}
#pragma mark - getters
- (ZYCTeaAttendanceClockInView *)clockInView {
    if (!_clockInView) {
        _clockInView = [[ZYCTeaAttendanceClockInView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight - kNavBarAndStatusBarHeight - 49.0f)];
        _clockInView.delegate = self;

    }
    return _clockInView;
}




- (ZYCTeaAttendanceStatisticsView *)statisticsView {
    if (!_statisticsView) {
        _statisticsView = [[ZYCTeaAttendanceStatisticsView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight - kNavBarAndStatusBarHeight - 49.0f)];
        _statisticsView.hidden = YES;
        _statisticsView.delegate = self;
    }
    return _statisticsView;
}

- (ZYCTeaAttendanceRecordBottomView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[ZYCTeaAttendanceRecordBottomView alloc] initWithFrame:CGRectMake(0, kScreenHeight - 49.0f, kScreenWidth,49.0f)];
        _bottomView.delegate = self;
    }
    return _bottomView;
}


- (NSDateFormatter *)dateFormatter {
    if (!_dateFormatter) {
        _dateFormatter = [[NSDateFormatter alloc] init];
        [_dateFormatter setDateFormat:@"yyyy-MM-dd"];
    }
    return _dateFormatter;
}







/*
 
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
